import greenfoot.*;  
import java.util.*;

/**
 * Water class represents parts of the flood. If constructor is called with allowFlooding=true this object will automatically reproduce itself
 * 
 * @author Jasper Jans
 */
public class Water extends Actor
{
    private long timeStarted = System.currentTimeMillis();  // timer functionality
        
    // flooding properties
    private Boolean allowFlooding;
    private Boolean reproduced = false;
    
    private int waveNum;  // the current wavenumber
    
    /*
     * Returns the current wavenumber
     */
    public int getwaveNum()
    {
        return waveNum;
    }
    
    /*
     * The Water constructor
     * @allowFlooding is set to true, this class will reproduce itself
     * @waveNum is used to create some performance optimalisation
     */
    public Water(Boolean allowFlooding, int waveNum)
    {
        this.allowFlooding = allowFlooding;
        this.waveNum = waveNum;
    }

    /*
     * Removes this object after 3 seconds 
     * If allowFlooding is set to true, this will also reproduce itself every 0.10 seconds increasing the wavenumber by 1
     * Reproduction is done by sets of two, one on the bottomleft side and one on the bottomright side, to simulate a sort of flood 
     */
    public void act() 
    {
        long currentTime = System.currentTimeMillis();
        long elapsedTime = currentTime - timeStarted;
        
        if(allowFlooding && elapsedTime / 10 >= 10)
        {
            if(getY() < 580 && !reproduced) // only start reproducing if there is some place left
            {
                reproduced = true;
                
                // check bottom-left and bottom-right to see if one of my neighbours have not already created a Water object
                // at that location containing the new wavenumber (older wavenumbers don't matter since they will be removed soon)
                List<Water> waterLeft = getWorld().getObjectsAt(getX() - 20, getY() + 40, Water.class);
                if(waterLeft.size() == 0 || (waterLeft.size() > 0 && ((Water)waterLeft.get(0)).getwaveNum() < waveNum))
                {
                    Water w1 = new Water(allowFlooding, waveNum);
                    getWorld().addObject(w1, getX() - 20, getY() + 40);
                }
               
                List<Water> waterRight = getWorld().getObjectsAt(getX() + 20, getY() + 40, Water.class); 
                if(waterRight.size() == 0 || (waterRight.size() > 0 && ((Water)waterRight.get(0)).getwaveNum() < waveNum))
                {
                    Water w2 = new Water(allowFlooding, waveNum);
                    getWorld().addObject(w2, getX() + 20, getY() + 40);
                }
            }
 
            GreenfootImage img = new GreenfootImage("water.png");
            setImage(img);
  
        }
        
        if(elapsedTime / 1000 >= 3) // remove me after 3 seconds to avoid memory issues
        {
            getWorld().removeObject(this);
        }
                  
    }    
}
